import axios from 'axios'
import {error} from "@/api/myMessage.js"
import {userStore} from "@/stores/userStore.js"
import {adminUrl, portalUrl} from "@/api/const.js";

export const successCode = 1000
export const errorCode = 2000
export const timeoutCode = 600//订阅超时
export const permissionDeniedCode = 601//无权限
export const notFond = 404

const urlPrefix = "/_api"
export const timeoutUrl = "/timeout"
axios.defaults.withCredentials = true;
axios.defaults.timeout = 30000;
axios.defaults.headers.post['Content-Type'] = 'application/json'
const instance = axios.create();
// axios.defaults.headers.post['Content-Type'] = 'multipart/form-data'
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'


//请求拦截器
instance.interceptors.request.use(
    (config) => {
        config.headers.Authorization = userStore().authorization;
        return config;
    },
    (err) => {
        error("请求发生错误，请检查后重试");
        return Promise.reject(err)
    }
)

//响应拦截器
instance.interceptors.response.use(
    result => {
        if (timeoutCode === result.data.code && result.data.message !== "") {
            location.href = adminUrl + timeoutUrl;//超时时间
        }
        if (permissionDeniedCode === result.data.code && result.data.message !== "") {
            error(result.data.message);
        }
        return Promise.resolve(result)
    },
    err => {
        let msg = "服务器发生错误，请稍后重试或联系管理员";
        error(msg);
        let errorObj = {
            data: "",
            code: notFond,
            message: msg
        }
        return Promise.reject(errorObj)
    }
)

export default {
    // get请求
    requestGet(url) {
        return new Promise((resolve, reject) => {
            instance.get(urlPrefix + url).then(res => {
                resolve(res.data)
            })
        })
    },
    // get请求 参数放请求体中
    requestGetBody(url, params = {}) {
        return new Promise((resolve, reject) => {
            instance.get(urlPrefix + url, {data: params}).then(res => {
                resolve(res.data)
            })
        })
    },
    // delete请求  参数放请求体中
    requestDeleteBody(url, params = {}) {
        return new Promise((resolve, reject) => {
            instance.delete(urlPrefix + url, {data: params}).then(res => {
                resolve(res.data)
            })
        })
    },
    // delete请求 参数是数组
    requestDeleteArr(url, paramName = "", arr = []) {
        let param = "?" + paramName + "=";
        arr.forEach(item => {
            if (arr !== null && arr.length > 0) {
                param += item;
                param += ",";
            }
        })
        param = param.slice(0, -1);
        return new Promise((resolve, reject) => {
            instance.delete(urlPrefix + url + param).then(res => {
                resolve(res.data)
            })
        })
    },
    requestDelete(url) {
        return new Promise((resolve, reject) => {
            instance.delete(urlPrefix + url).then(res => {
                resolve(res.data)
            })
        })
    },
    // post请求
    requestPost(url, params = {}) {
        return new Promise((resolve, reject) => {
            instance.post(urlPrefix + url, params).then(res => {
                resolve(res.data)
            })
        })
    },
    // put请求
    requestPut(url, params = {}) {
        return new Promise((resolve, reject) => {
            instance.put(urlPrefix + url, params).then(res => {
                resolve(res.data)
            })
        })
    }

}
